<?php

namespace App\Model\Api;

use App\Model\BaseModel;
use App\Model\GiftLibrary;
use Illuminate\Database\Eloquent\Model;

class GiftAppoint extends BaseModel
{
    protected $connection = 'api';
    protected $table = "gift_appoint";
    private $fields = ['*'];

    public function getList($page, $limit) {
        $query = new self();

        return $query->with(['giftLibrary:gift_id,name'])->orderBy('sort', 'desc')->paginate($limit, $this->fields, 'page', $page);
    }

    public function createOrUpdateData(int $gift_id, int $type, int $sort, $start_time, $end_time, int $id = 0) {
        return curl_request(getKkaudioApiUrl().'/inner/gift/appoint/upsert', [
            'id' => $id > 0 ? $id : 0,
            'gift_id' => $gift_id,
            'type' => $type,
            'sort' => $sort,
            'start_time' => $start_time,
            'end_time' => $end_time
        ]);
    }

    public function deleteData(int $id) {
        return curl_request(getKkaudioApiUrl().'/inner/gift/appoint/remove', [
            'id' => $id
        ], 'urlencoded', 'GET');
    }

    public function giftLibrary() {
        return $this->belongsTo(GiftLibrary::class, 'gift_id', 'gift_id')->withDefault([
            'name' => '空'
        ]);
    }
}
